package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/protobuf/types/known/emptypb"
	"sale_system/goods_srv/proto"
)

var client proto.GoodsClient
var conn *grpc.ClientConn

func Init() {
	var err error

	conn, err = grpc.Dial("192.168.56.103:58852", grpc.WithInsecure())
	if err != nil {
		panic("failed to connect, " + err.Error())
	}

	client = proto.NewGoodsClient(conn)
}

func TestGetAllCategorysList() {

	rsp, err := client.GetAllCategorysList(context.Background(), &emptypb.Empty{})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp.Total)
	fmt.Println(rsp.JsonData)
}

func TestGetBannerList() {

	rsp, err := client.BannerList(context.Background(), &emptypb.Empty{})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp.Total)

}

func TestSubCategory() {
	rsp, err := client.GetSubCategory(context.Background(), &proto.CategoryListRequest{
		Id: 135492,
	})
	if err != nil {
		panic(err)
	}

	fmt.Println(rsp.SubCategorys)
}

func TestCategoryBrand() {

	rsp, err := client.CategoryBrandList(context.Background(), &proto.CategoryBrandFilterRequest{})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp.Total)

	for i := 0; i < 30; i++ { // 注意这个测试过程中可能越界访问，因为我们设置的分页参数，可能数据小于30

		fmt.Println(rsp.Data[i].Category)
		fmt.Println(rsp.Data[i].Brand)
	}
}

func TestGetCategoryBrandList() {
	rsp, err := client.GetCategoryBrandList(context.Background(), &proto.CategoryInfoRequest{
		Id: 130368,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp.Total)
	fmt.Println(rsp.Data)
}

func TestGetGoodsList() {
	rsp, err := client.GoodsList(context.Background(), &proto.GoodsFilterRequest{
		TopCategory: 130361,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp.Total)

	for _, good := range rsp.Data {

		fmt.Println(good.Name, good.ShopPrice, good.Id)
	}
}

func TestBatchGetGoodsList() {

	rsp, err := client.BatchGetGoods(context.Background(), &proto.BatchGoodsIdInfo{

		Id: []int32{518, 523, 524},
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp.Total)

	for _, good := range rsp.Data {
		fmt.Println(good.Name, good.ShopPrice, good.Id)
	}
}

func TestGoodDetail() {
	rsp, err := client.GetGoodsDetail(context.Background(), &proto.GoodInfoRequest{
		Id: 518,
	})
	if err != nil {
		panic(err)
	}
	fmt.Println(rsp)
}

func main() {

	Init()
	defer conn.Close()
	//TestGoodDetail()

	TestCategoryBrand()
}
